package com.atguigu.thread2;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SemaphoreTest {
    private static Lock lock = new ReentrantLock();
    private static Semaphore semaphore = new Semaphore(3);

    public static void main(String[] args) {
        for (int i = 0; i < 7; i++) {
            lock.lock();
            int finalI = i;
            try{
                new Thread(()->{
                    try {
                        semaphore.acquire();
                        System.out.println(finalI);
                        TimeUnit.SECONDS.sleep(3);
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {

                    };

                },i+"").start();


            }catch(Exception e){
                e.printStackTrace();
            }finally {

                lock.unlock();
            }
        }

    }
}
